package com.joyer.core.valid;

import com.joyer.exception.BusinessException;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * User: wangzhiyuan Date: 2014/10/13 Time: 13:30
 */
@Component
public class InterfaceUtil {

	public void checkParamsValidate(BindingResult br) throws BusinessException {
		ValidatedStatusEntry dto = this.getParamsValidatedStatus(br);
		if (dto.isHasErrors()) {
			Map<String, Object> result = dto.getResult();
			throw new BusinessException((String) result.get("errmsg"), (String) result.get("errcode"));
		}
	}

	/**
	 * 返回验证结果
	 *
	 * @param br
	 * @return
	 */
	public ValidatedStatusEntry getParamsValidatedStatus(BindingResult br,String...strings) {
		ValidatedStatusEntry dto = new ValidatedStatusEntry();
		if (br.hasErrors()) {
			Map<String, Object> result = new HashMap<String, Object>();
			StringBuffer errorMessage = new StringBuffer();
			for (FieldError oe : br.getFieldErrors()) {
				errorMessage.append(oe.getField() + ":");
				errorMessage.append(oe.getDefaultMessage() + ";");
			}
			result.put("status", "1");
			result.put("errcode", "100");
			result.put("errmsg", errorMessage.toString());
			if(strings != null && strings.length >= 2 ){
				result.put("action", strings[0]);	//三种提示方式，dialog1（1按键），dialog2（2按键），toast方式，如果该字段为空则不显示
				result.put("extend", strings[1]);	//扩展字段，一般不用，内容为json串，特殊业务做具体返回
			}
			dto.setHasErrors(true);
			dto.setResult(result);
		} else {
			dto.setHasErrors(false);
		}
		return dto;
	}

	/**
	 * 将一个对象转换成手机接口格式返回
	 * 
	 * @param errCode
	 *            错误状态码 103开始
	 * @param errMsg
	 *            错误信息
	 * @return
	 */
	public Map<String, Object> getReturnMap4Error(String errCode, String errMsg,String...string) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("status", "1");	//0为正常返回，>0为业务错误,<0为系统错误
		result.put("errcode", errCode);
		result.put("errmsg", errMsg);
		if(string != null && string.length >= 2 ){
			result.put("action", string[0]);	//三种提示方式，dialog1（1按键），dialog2（2按键），toast方式，如果该字段为空则不显示 upgrade强制升级
			result.put("extend", string[1]);	//扩展字段，一般不用，内容为json串，特殊业务做具体返回
		}
		return result;
	}
	

	/**
	 * 只返回成功状态
	 * 
	 * @return
	 */
	public Map<String, Object> getReturnMap4SuccessStatus() {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("status", "0");
		return result;
	}

	/**
	 * 只返回空结果状态
	 * 
	 * @return
	 */
	public Map<String, Object> getReturnMap4EmptyStatus() {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("status", "2");
		return result;
	}

	/**
	 * 将一个map转为手机接口格式返回
	 * 
	 * @param map
	 * @return
	 */
	public Map<String, Object> getReturnMap4Map(Map map) {
		Map<String, Object> result = new HashMap<String, Object>();
		if (map == null) {
			result.put("status", "2");
		} else {
			result.put("status", "0");
			result.put("result", map);
		}
		return result;
	}

	/**
	 * 将一个对象转换成手机接口格式返回
	 * 
	 * @param obj
	 * @return
	 */
	public Map<String, Object> getReturnMap4Object(Object obj) {
		Map<String, Object> result = new HashMap<String, Object>();
		if (obj == null) {
			result.put("status", "2");
		} else {
			result.put("status", "0");
			result.put("result", obj);
		}
		return result;
	}

	/**
	 * 将一个不带分页的list转换成手机接口格式返回
	 * 
	 * @param list
	 * @return
	 */
	public Map<String, Object> getReturnMap4List(List list) {
		Map<String, Object> result = new HashMap<String, Object>();
		if (list == null || list.size() == 0) {
			result.put("status", "2");
		} else {
			Map<String, Object> items = new HashMap<String, Object>();
			items.put("items", list);
			result.put("status", "0");
			result.put("result", items);
		}
		return result;
	}

	/**
	 * 将一个不带分页的list封装，不加items一级
	 * 
	 * @param list
	 * @return
	 */
	public Map<String, Object> getReturnMap4DirectList(List list) {
		Map<String, Object> result = new HashMap<String, Object>();
		if (list == null || list.size() == 0) {
			result.put("status", "2");
		} else {
			result.put("status", "0");
			result.put("result", list);
		}
		return result;
	}

	/**
	 * 将一个带分页的list转换成手机接口格式返回
	 * 
	 * @param list
	 *            分页条件后返回的list
	 * @param total
	 *            列表总数
	 * @return
	 */
	public Map<String, Object> getReturnMap4PagingList(List list, int total) {
		Map<String, Object> result = new HashMap<String, Object>();
		if (list == null || total == 0) {
			result.put("status", "2");
		} else {
			Map<String, Object> items = new HashMap<String, Object>();
			items.put("items", list);
			items.put("total", total);
			result.put("status", "0");
			result.put("result", items);
		}
		return result;
	}
}
